#!/usr/bin/env python3
import nmap
import sys
def nmap_ping_scan(network_prefix):#network_prefix为一个网段 0.0.0.0/24
    nm = nmap.PortScanner()#调用PortScanner方法
    ping_scan_result = nm.scan(hosts = network_prefix, arguments = '-v -n -sn')
    #-v 提高输出信息的详细程度
    #-n 不做DNS解析，反向域名解析比较慢
    host_list = []
    #print(ping_scan_result)
    for result in ping_scan_result['scan'].values():
        if result['status']['state'] == 'up':
            host_list.append(result['addresses']['ipv4'])
    return host_list

if __name__ =="__main__":
    for host in nmap_ping_scan(sys.argv[1]):
        print("%-20s  %5s"%(host,'is UP'))
